//
//  LGCommissionCell.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/26.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "LGCommissionCell.h"
#import "LGCommodityInfoCell.h"
#import "LGImageManagerIO.h"
#import "LGPopView.h"
#import "LGPhotoFlowView.h"

@interface LGCommissionCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@end

@implementation LGCommissionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.desTextView.text = nil;
    self.desTextView.placeholder = @"描述下你的包包吧";
    self.desTextView.placeHolderColor = TEXT_COLOR_2;
    self.desTextView.textColor = TEXT_COLOR_1;
    self.desTextView.font = FONT(15);
    self.desTextView.maxNum = 200;
    self.countLab.text = [NSString stringWithFormat:@"%lu/%lu",self.desTextView.text.length,self.desTextView.maxNum];
    WeakObj(self);
    self.desTextView.changeEditing = ^(LGPlaceholderTextView *textView) {
        selfWeak.countLab.text = [NSString stringWithFormat:@"%lu/%lu",textView.text.length,textView.maxNum];
    };
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //设置滚动方向
    /**
     UICollectionViewScrollDirectionVertical, 垂直滚动(默认)
     UICollectionViewScrollDirectionHorizontal 水平滚动
     */
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //上下边距
    layout.minimumLineSpacing = 15;
    //左右边距
    layout.minimumInteritemSpacing = 0;
    //每组内容的缩进
    layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    /*
     以垂直方向滑动为例子
     cellCount = (CollectionViewContentWidth-sectionInset.left-sectionInset.right+minimumInteritemSpacing)/(itemSize.width+minimumInteritemSpacing)
     */
    
    //缓存高度
    layout.itemSize = CGSizeMake(self.collectionView.height, self.collectionView.height);
    
    self.collectionView.collectionViewLayout = layout;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"LGCommodityInfoCell" bundle:nil] forCellWithReuseIdentifier:@"LGCommodityInfoCell"];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return (self.dataArray.count < 12)?(self.dataArray.count + 1):(self.dataArray.count);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGCommodityInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LGCommodityInfoCell" forIndexPath:indexPath];
    if (indexPath.row == self.dataArray.count)
    {
        [cell isChooseUI:YES];
    }
    else
    {
        [cell isChooseUI:NO];
        [LGImageManagerIO thumbnailForAsset:self.dataArray[indexPath.row] size:CGSizeMake(self.collectionView.height, self.collectionView.height) resultHandler:^(UIImage *result) {
            cell.photo.image = result;
        }];
    }
    WeakObj(self);
    cell.deleteAciton = ^(LGCommodityInfoCell *cell) {
        NSIndexPath *index = [selfWeak.collectionView indexPathForCell:cell];
        [selfWeak.dataArray removeObjectAtIndex:indexPath.row];
        if (index.row == selfWeak.dataArray.count)
        {
            [selfWeak.collectionView reloadItemsAtIndexPaths:@[index]];
        }
        else
        {
            [selfWeak.collectionView deleteItemsAtIndexPaths:@[index]];
        }
        
        
        LGCommodityInfoCell *lastCell = [selfWeak.collectionView.visibleCells lastObject];
        [lastCell setPhotoCount:selfWeak.dataArray.count];
    };
    
    [cell setPhotoCount:self.dataArray.count];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.dataArray.count)
    {
        if (self.choosePhoto)
        {
            self.choosePhoto(self);
        }
    }
    else
    {
        
        if (self.browsePhoto)
        {
            self.browsePhoto(self,indexPath.row);
        }
    }
}

//查看拍照流程
- (IBAction)checkAction:(id)sender
{
    
    LGPopView *pop = [LGPopView new];
    LGPhotoFlowView *view = [LGPhotoFlowView createFromXib];
    [pop showOnWindowWithContentView:view];
    [self endEditing:YES];
    [pop showWithDidShowBlock:^(LGPopView *view) {
        
    }];
    
    view.closeAction = ^(LGPhotoFlowView *view) {
        [pop hiddenWithDidHiddenBlock:nil];
    };
    //    if (self.checkPhoto)
    //    {
    //        self.checkPhoto();
    //    }
}
//选择品牌
- (IBAction)chooseBrandAciton:(id)sender
{
    if (self.chooseBrand)
    {
        self.chooseBrand(self);
    }
}

//选择款式
- (IBAction)chooseStyleAciton:(id)sender
{
    if (self.chooseStyle)
    {
        self.chooseStyle(self);
    }
}


- (void)addPhotoAndRefreshUIWith:(NSArray <PHAsset *>*)photos
{
    [self.dataArray addObjectsFromArray:photos];
    [self.collectionView reloadData];
    NSInteger index = (self.dataArray.count < 12)?(self.dataArray.count):(self.dataArray.count - 1);
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
}

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil)
    {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
